package com.york.user.controller;

import com.york.redis.util.CacheUtil;
import com.york.redis.util.RedisShareLockUtil;
import com.york.redis.util.RedisUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class TestController {

    /**
     * 解耦，redisUtil已经封装了底层对redis的操作
     */
    @Resource
    private RedisUtil redisUtil;

    @Resource
    private RedisShareLockUtil redisShareLockUtil;

    @Resource
    private CacheUtil cacheUtil;

    @GetMapping("/testLocalCache")
    public void testLocalCache(List<Long> skuIdList) {
        cacheUtil.getResult(skuIdList, "skuInfo.skuName", SkuInfo.class, (list) -> {
            Map<Long, SkuInfo> skuInfo = getSkuInfo(skuIdList);
            return skuInfo;
        });
        cacheUtil.getResult(skuIdList, "skuInfo.skuPrice", SkuPrice.class, (list) -> {
            Map<Long, SkuPrice> skuPrice = getSkuPrice(skuIdList);
            return skuPrice;
        });
    }

    // 模拟RPC接口(可以使用OpenFeign来作为Service层的RPC调用)
    public Map<Long, SkuInfo> getSkuInfo(List<Long> skuIdList) {
        return Collections.emptyMap();
    }

    public Map<Long, SkuPrice> getSkuPrice(List<Long> skuIdList) {
        return Collections.emptyMap();
    }

    class SkuInfo {
        private Long id;
        private String name;
    }

    class SkuPrice {
        private Long id;
        private Double price;
    }

    @RequestMapping("/test")
    public String test() {
        return "hello,world";
    }

    @GetMapping("/testRedis")
    public void testRedis() {
        redisUtil.set("name", "York");
    }


    @GetMapping("/testRedisShareLock")
    public String testRedisShareLock() {
        boolean result = redisShareLockUtil.lock("York", "123456", 100000L);
        System.out.println("分布式锁获取" + result);
        return String.valueOf(result);
    }
}
